package bsc.war.form;

import bsc.ejb.entity.Person;
import bsc.ejb.entity.PersonScoreDomain;
import bsc.ejb.entity.ScoreDomain;
import bsc.ejb.metadata.License;
import bsc.war.delegate.DelegateBSC;
import bsc.war.factory.Factory;
import bsc.war.service.BSCUtils;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.lang.math.NumberUtils;
import org.jasypt.util.text.BasicTextEncryptor;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;

/**
 *
 * @author memozac
 */
public class NewPerson extends AbstractController {

    @Override
    protected ModelAndView handleRequestInternal(HttpServletRequest request,
            HttpServletResponse response) throws Exception {

        try {
            DelegateBSC delegate = (DelegateBSC) Factory.getBean("DelegateBSC");


            boolean edit = false;
            Long idPerson = -1L;

            if (NumberUtils.isDigits(request.getParameter("idPerson"))) {
                Long id = Long.valueOf(request.getParameter("idPerson"));
                if (id >= 0) {
                    edit = true;
                    idPerson = id;
                }
            }


            Person _person = delegate.findPerson(request.getParameter("username"), request.getParameter("password"));
            if(_person != null && !edit){
                request.setAttribute("personExists", true);
                return new ModelAndView("newPerson");
            }

            if(!edit){
                License license  = delegate.findCurrentLicense();
                if(license != null){
                    List<Person> persons = delegate.findPerson();
                    BasicTextEncryptor textEncryptor = new BasicTextEncryptor();
                    textEncryptor.setPassword(BSCUtils.ENCRYPT_HASH_KEY);
                    Long licenseNumbers = Long.parseLong(textEncryptor.decrypt(license.getLicense()));
                    if(licenseNumbers != -1 && persons.size() >= licenseNumbers ){
                        request.setAttribute("licenseExceeded", true);
                        return new ModelAndView("newPerson");
                    }
                }
            }

            Person person = edit ? delegate.findPerson(idPerson) : (Person) Factory.getBean("Person");

            person.setEmail(request.getParameter("email"));
            person.setName(request.getParameter("name"));
            person.setPassword(request.getParameter("password"));
            person.setUsername(request.getParameter("username"));
            Person.Profile profile = Person.Profile.valueOf(request.getParameter("profile"));
            person.setProfile(profile);

            if (edit) {
                delegate.updatePerson(person);
                request.setAttribute("edit",Boolean.TRUE);
            } else {
                delegate.createPerson(person);
            }

            if (profile != Person.Profile.NORMAL_USER) {
                //Remove all relations of that person
                List<PersonScoreDomain> personScoreDomains = delegate.findPersonScoreDomain(person);
                for (PersonScoreDomain personScoreDomain : personScoreDomains) {

                    //if (personScoreDomain.getProfile() == PersonScoreDomain.Profile.FINAL_USER || personScoreDomain.getProfile() == PersonScoreDomain.Profile.CORE_TEAM) {

                        delegate.deletePersonScoreDomain(personScoreDomain);

                    //}
                }

                ScoreDomain scoreDomain = delegate.findScoreDomain(1L);
                PersonScoreDomain personScoreDomain = (PersonScoreDomain) Factory.getBean("PersonScoreDomain");
                personScoreDomain.setPerson(person);
                personScoreDomain.setScoreDomain(scoreDomain);
                personScoreDomain.setDefaultDomain(true);
                if (profile == Person.Profile.ADMINISTRATOR) {
                    personScoreDomain.setProfile(PersonScoreDomain.Profile.ADMINISTRATOR);
                } else if (profile == Person.Profile.DESIGNER) {
                    personScoreDomain.setProfile(PersonScoreDomain.Profile.DESIGNER);
                } else if (profile == Person.Profile.DATA_MANAGER) {
                    personScoreDomain.setProfile(PersonScoreDomain.Profile.DATA_MANAGER);
                }
                delegate.updatePersonScoreDomain(personScoreDomain);
            } else {
                //Remove all relations of that person
                List<PersonScoreDomain> personScoreDomains = delegate.findPersonScoreDomain(person);
                for (PersonScoreDomain personScoreDomain : personScoreDomains) {
                    if (personScoreDomain.getProfile() != PersonScoreDomain.Profile.FINAL_USER && personScoreDomain.getProfile() != PersonScoreDomain.Profile.CORE_TEAM) {
                        delegate.deletePersonScoreDomain(personScoreDomain);
                    }
                }
            }

        } catch (Exception e) {
            e.printStackTrace();
        }

        return new ModelAndView("newPerson");
    }
}